package com.paic.custserv.authentication;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.custserv.common.Constant;
import com.paic.custserv.domain.AppUser;
import com.paic.custserv.service.DefaultUserDetailService;
import com.paic.custserv.utils.HttpClientUtil;
import com.paic.custserv.utils.PropertiesHolder;

public class LoginAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
	
	private static final Logger logger = LoggerFactory.getLogger(LoginAuthenticationProvider.class);
	
	private Md5PasswordEncoder passwordEncoder;
	
	private DefaultUserDetailService userDetailService;
	
	@Override
	protected UserDetails retrieveUser(String username,
			UsernamePasswordAuthenticationToken authentication)
			throws AuthenticationException {
		UserDetails loadedUser;
        try {
            loadedUser = userLoginAuth(authentication);
        } catch (com.paic.custserv.authentication.AuthenticationException e) {
        	throw e;
        }
        return loadedUser;
	}
	
	/**
	 * 用户验证接口
	 * author zhouhuiqun470
	 * @param url
	 * @param userVO
	 * @return
	 */
	private AppUser userLoginAuth(UsernamePasswordAuthenticationToken authentication) throws com.paic.custserv.authentication.AuthenticationException {
		String account = authentication.getPrincipal().toString();
		String presentedPassword = authentication.getCredentials().toString();
		AppUser user = userDetailService.loadUserByUserrole("LEADER_CUSTOMER");
		user.setUserRole("LEADER_CUSTOMER");
		user.setUserUMID("TANGSHENG560");
		user.setUserName("张三五");
		return user;
	}

	public Md5PasswordEncoder getPasswordEncoder() {
		return passwordEncoder;
	}

	public void setPasswordEncoder(Md5PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}

	public DefaultUserDetailService getUserDetailService() {
		return userDetailService;
	}

	public void setUserDetailService(DefaultUserDetailService userDetailService) {
		this.userDetailService = userDetailService;
	}

	@Override
	protected void additionalAuthenticationChecks(UserDetails userDetails,
			UsernamePasswordAuthenticationToken authentication)
			throws AuthenticationException {
		
	}
	
}
